package cn.smbms.controller.user;

import cn.smbms.pojo.User;
import cn.smbms.service.user.UserService;
import cn.smbms.tools.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;


@Controller
@SessionAttributes( value = {Constants.USER_SESSION})
public class LoginController {

	@Autowired
	private UserService userService;
	@RequestMapping(value = "/login.do",method = {RequestMethod.POST,RequestMethod.GET})
	public String postlogin(@RequestParam("userCode") String userCode, @RequestParam("userPassword") String userPassword, Model model){
		//调用service方法，进行用户匹配
		User user = userService.login(userCode,userPassword);
		if(null != user){//登录成功
			//放入session
			model.addAttribute(Constants.USER_SESSION,user);
			//页面跳转（frame.jsp）

			return "redirect:/jsp/frame.jsp";
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			model.addAttribute("error", "用户名或密码不正确");
			return "forward:/login.jsp";
		}

	}

}
